/*    Copyright 2010 Tobias Marschall
 *
 *    This file is part of MoSDi.
 *
 *    MoSDi is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    MoSDi is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with MoSDi.  If not, see <http://www.gnu.org/licenses/>.
 */

package mosdi.tests;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;
import mosdi.fa.Alphabet;
import mosdi.fa.CDFA;
import mosdi.fa.DFAFactory;
import mosdi.fa.FiniteMemoryTextModel;
import mosdi.fa.GeneralizedString;
import mosdi.fa.MarkovianTextModel;
import mosdi.fa.ForwardPatternExpectationCalculator;
import mosdi.fa.PatternExpectationCalculator;
import mosdi.paa.MatchCountDAA;
import mosdi.paa.PAA;
import mosdi.paa.TextBasedPAA;
import mosdi.util.Iupac;

public class PatternExpectationCalculatorTest extends TestCase {
	
	private final String background = "GGTAGGCTCGTGCAAAGGCGGGTTCTTCTCTTCGCAAGATTTAATAGTACTGTTTTACGCTTGTACGACCATAATGAATGTTCAATCTCTATAAGCATTTCAGTGTTAAGTACTGCGTGATCAATGCTGGTTTCCGGGCCTAAATAGGCGCCGACATCCATTCTCTTTGGCCTCGGAGCAATGACCTGGTATCAGATTCAACTAGGAGTCCTCGGTTTATATACTCGATACCGTGTTCCGTTAAATGGGACCGTGTAAGGAAGTCGAATCGTAGATACTAGCATCGGGACCACTTACGGCAGGACAAGCCATTCCCGGGCGCTATGTCAGTCCGTCGTATAACTATTATACCTTCACACCTCTCCGACCGGCTGCATACCGCGTAGTCCCACATAAGAACGTATCCTTGCATTTGGTATTCGCTCCTCTATGGGTTACTAATTAACGATTTCTGATACTTGATATTTCCTATAACCTAAGCAGGCCATAAACAATGCGAGAGCATACCGGCGGCAATCCTTTTCACTACTGGTATTCGACGTTACTGACAATGAAGGTAGAGTCGGACCGTTACGTTAAAGTGAACGCGCAGGACAAGAGGTACGTGGTGCGCGATAAGGAGGCCCCGAGCGACTGGAGGGTTGCTTAGGATAACGGACGGGGAAAATGGAAGCGCGTATATTGACACTCTTGACCTGACGCGACAGCCCCTTAACGACCCTACTTGGTTATTTCCTTCATGAATTTTGTGCAGCAAGTGGGTGAGCGCCACATTCTTCCGAAGCCGGTTCCCGAGATTGAACCCCGACTTGGCTATATACCGCATGTCATAGCTACGGATGACTACCAGGAATGGCCAAACGCTCAGGCTCTGCTGATGCATCTAAATACCAGTAGCGCCGATCCAACCCTCGATTACTGGTTTTCTTTTCGAGTAGAAACTTAAGAGCCACAACTGGTACACCGGCGCTCTATAGCCATAGCTCACCGTTCACATAGCGACGGCTGGCACTTGGTTCACGAGAATCTAAACGTCACTGCAGAGAGTCGCACAGCCTTTGATCTATGGTATCTTGCGGTCCCAATTGCTTTTGTAAGTAATAACGTGGTCTGTGCCTCTTGGTGAACGACGCGATGCACCTGAGAATTGAAGTAAGAGCATGTATTGACGTACCTCGGGAAGGGATATATGCCGCCTTGATCACAAGGTACGCTAGTAAGGCCGGTAAATCTCTACTCATCCGCGCAACCCCGTGTTGTAGTATTAGTATAGGTTGCTTCGCTCCAGTGGGCAGACGATCTAACCGAGCGGACTCCAACATCAGCAATGGTCATTGGTAGAAATGCCGGTTTTCGTAAAGGGGCATTCACTTAGAGTCCGCCGGATATGACTAATGGACTATTTTTTGAAGGTTTAGCAGCTTTGTGCGGGTTTTGAGCAAGACACGAAAAAATAGGAGCGACTAAAATAAGAAGGGTTGCGGGTGCTAAATCCCTGCGATGAAGCGAGTAGATACTTTAGTTTGATTAGAGCTGACACCTGAGGGGTCTTACACCCATTAGCCCTGTTAGCTAACTCTATAACTTCTGTGCCATGTTGAAATCTGCAGCTCAACGTAGACCTCTACACCCAGGTCCTACACGGTAACTAAAAGGAGCAGACTCCAGCTCCCCTGGTAACCCACTAGTGCAAAGGTCTCCCTTGAAAGGTCACGCTATCGGAGTTTGACTCCTGAGGATACGGACGGCCATGAAGATTCCAGTAGGATGGCGCTGTGGCGGTCCCATATTCTTAGGTCAGATGGAACCTTCCTGATGTGCGCCGTAGTCACAAATGCGCCCATACTTACTGAGGTGCACTGAGCAAGCGAGCCGAGAAGGCGTTTCACGTCAAGTACTGTAGCAACACGCTGATCTGTTAATTTGCTAGGCGTTGTAGTCCTCGGAGGGCCACATACCGGCGCACGACCGGTTGCAAAGATGCAAAATTGGAGCCGATAACATTGAAGTAGAAATCTAAAGACACGTAGGTTCTCGTGCGGTTTTGAGGGTTGGGCGCCGATGTAAGTACGCTCGTGATCGACACGCATCTCGAATCCAAGTACATAAGCTGACATTATATGGTGTCGTCGTATGGGGGTGGGCAATGGCCCAAAGCCGCTGCACTTCTGATGCTAAACCAGTCAAGACTCCGAACCTAGGCAAGAACTCCTAGATCATAACACACAGTTATGATGATCGGTCGGTTATAGCTGTTCCAATAGGCCAGAGAGAAACCGAGTCCACGAATACAAGAACAACCTTGGGTGCCTGTAGGGTCAAACTTTGTCCATCGTACTGAAGCTCAACCGTATTAGGTTTCTTTTCGTCGGACCAGATGAGTGGAACCCTGGCGATCGGTTTACTTGCCATAGCGCCTCCATCGGATAGGTAACCCAAGGCTGAAGGCAACATTAGTATTGCTACAAGCTTGTAGGTGCGATTTCAGACGTCTATGCTCGCGTGAGTCCCGGTCGACTGCATAAGATTTTCTACTCCATCCGAGAAGTTATGACAAAAAGTCGAGCGTAATCAAGATGTATGTGCACGCGACTTTGTGAGACCCGCGGACGACTCCTGGCAAGACCCGAAAATCATTGGAGTGGTGAGGACCCGCCCTGAACATGACTCAACGCACGCTACTAGAATTGTGAACGGAAATCTATAATGTTGGAGACTATAAGGCGCCTGATCTCCAAACACCCGTTCGAGCGTTTTGTGCACGTAGCAACCCCCGAGTCGGGCGGGTCATAAAACGAGTTTTTCGAATGGCATGATGGCGCGGCTTTCTATGGAGGGGTTCGTTTCCGAAAATGCATTAGGAGGAAACAAAATACTCAGCCTCTCGCCACGAGTACATCATTAGGTCCTGAGCGGTATTCTCAATAACTGTGCTCCGTACATTCCGAACGAATATCGGGGATCAACAGACGCGCTACGGTGCTCATGTCATTAAGGCTCGTAAGGACGAGACGAGCTGGATGTGCACAAGCACATTGAACTCTAGCTAGTGGCATCTTCTGTCAAGTGGTTAGCACTATAGCTTTGACAAGCTTTACCTCCTCGCATCTCCACCCTTAGCTTACAGTTCTTCGAAATCGACCAAGCGCCGACTGGATGCGGAAACGTATGCAGCAACAGGCAAATGGGTGTTACCCTGATAGATCATCCCCCAGCGGTCCCCCGCCTTAGACATTGTGGCTGCCAAGATTTACGAATGCAACGCCGGACTGTCGCTCGTCTTTACAGGTGGGAGCTCTTGCATCTGGACCCGATACATTGAATTAGGTTGCCGTGTCCCCGACCGACCACTTTAAAGACGACCGTAACGGTCGTATCTCGAGATAATCCGGTGACCCGCTGGCGGGCGTCTCCCAGCGCGGAAGAGACCAGACTGAGGAGTGGACATAAGGGCTTCAAGTCGGCGTTCTTTCTATCGCGTTACGTGTCCAGCCATACGGGTCAGAAACAAAAAGACATGGGGCCGGAGATTAAGTAGGAATCAGTCACCCTTGGAACCTAACTTATCAGGGGGTCTAGCGAGATAATAGCCTATGGCCTGAGCACTGACTACGTTTGGAGTGGCTGGCATAGTCTCTTGCAGGTTGCTTTTCGTTGATTTCCCGAGAAACAGCATGATAGTTGAGGTTACGGCCGCCCATTCGATTAAAACTCAGCATTTTATCGAAAGCCGCATAATGCGGAGCAACGCGGATAACTGATGTGCATGCAAGATAGGCTGTAGGCTGCTTCAACCCATCGGCAGTGGACACTTAGCGGGAGCTCTAAGAAATATACGTGAGTATAATCCATAATACGCTCGCCGGCACAAGCACGTACCCCGATCCATAGTCTCACGTCCCCTCGGCCGTAAGTGCAGAATTTGTTGACTCGATCCATACGAGTCAAACCTTACGGCGTTAATCTTCAGAACCGGCGTCGTCCACATTCAAATCTAGTTGTGCTAGGACGCACAAAGCCTTATACAGATCTCGAAGTGCCAGTTCAGATGTAGCGAGAACGTATCGACGTGCCAGAGGTCAGTTACGTGAAACACTGCAGGGATTGTGCAAGACCGCGTAGGCACTTACTCGATTCCCCACGATGATCGCGTAGATAGAATTACAACGGGTTTTAACCGTTATAGCGCGTGAGAGTGGGAAAGCACCCTCCCCTGCTAGTATATGTTAGTCACTTCCTTGTACATCGGACTTAGTGATTAGATGTTCGGGACCTCTTCTGATTTGCTCGCTCTCTCGCCAAGATGTGGCTGACGGAGTATGGTACAGAGCGCGCAAGAGGACCGTCCCATGTCCTAAGGAAGCGACTTTGCCCGTTATGGACTGTTCCGGTGAGCCGTCAATGGCCTCGTACAACTTAAAGGGGGTGTGTCTTGTGTAATCCCGCCCTCGGGCAAATGGTTCTGCAATGCAGTCCCTGCGAGGGAGGGCCTTAGGAGGTGCGTTCGCGACATAGAATTCGATTCAGAGTTTTGGGCTACTACCCCGAGTATCATCTGCTGCCGAGATAGGTTAAGCAAGCGCTCTAATCTCGGAGTTTACCGTTTATGTTCAGCCAAACTCACAGCGGTTGCGTCTATACGCAAGTCGGTCAGCTCCACATACTCTATTCCGGCTACGGGTGCGGTGGGTTGTATCCGATTCCGTAGCGCGTTATGCAGTCTCACTGAACGGATGGCAGAATACGTTAGACTAGGGGTCCTTTACGACAGCGCTCCCCGTCTGTAACAATTGCTGGTATTTAAAAGACTGAGTACACATTCACAGGGGTCGAGGTGACGCTCGATAGTAATCTTGCAGTGTACTGTGAACGTAGCAATGCCCCTATCGCTGCTTGTGTGGTAAACAAATCACATAGAAGTACTGCTCCAGAGGGGGACCTCCTCGCTTCTCCTGGCGCCCACAATCCTGAAATGCTTCTTTGAGCCAATTGCCGATCCTAGTCTGTTCAAGGGCGAACCAGGCTTAACTGTATGTTAGTCCTGCCTGTCAGCGGTAGGTAAGAGTGCAACCGATTCTCAGTCATATGCCCTTGCCTGAGCCATACATCGTGGGGGAGTTCGGTCTGAATGTCTGTCCGGTATTGGTATGTCAAGGTTGCACCAACGCCTCAGACCTCACGAGATCGGCCAACCCTAAACCCAAGCGCGTCTCAGCCAGGTGTCGAATCAACAACACAGAAGTGATAAAAAAATACAGAGTTCACATAAAGCGCCTTCTTCGCGCTGCCGATGCAAGTAACCGTAGGATCGCGGTGGGTTCGTTAATTTGACGAGCTACCCGCAAACGACGAGACTACGTCGTGTCGGTTTCGCGTTTACCTAATCGCTATCCCCAAGGATGACTGGCGCACGTCGGTTTGCGCGGCTAAGGTATAATGCATAGTATAAGTCTTACAACGCCCACGACTTCACGAAGAGATACGTGGTGAGCTTCACTTAAGGCCTTATAGGGCCGCTCCTGTGGCCCCCTCAGTCGAAGGCCTCTCCTCTTCACATGCTAGATGCAACTCTAATCATCTTTATGCCCCATACGATTGAGTCGTGACTCTGAGATCGGCGTAGCCGGCACTAATAAATGAAGTTAAGGCCGTTGGTGTCCATGGATTTCGGGACGTCCTAGTCGAAATAAGTCAAGGATTTTGTGTTATGGTGCCCGCTGAGCCACGGATATGACGTCCTCTGAGTTAGTAGCTAAAGTTGTGAACTATGGGTGACTTAGTTTGGGGTTACTGGCCCAAAGGCGGCTTCTGACAGAACCCCGAGAGCGCAGCTTTTGCGAAGGTCACACGCGGTTGAGTACACGTTACTTCAAAGCGCGGGTGGATTTATAAGAGTTGGGGTCTATACGCGGGCACTACGGTTAGCTTACCGTCTTTAGTGAGCGTATCACCTACCGTCCAGGTAGAACCTCATTCTGGACACAGTCAGGACTGAGGTTAGCGGTTATTAAAGGGTAGCTCGCCGCCTGTTTTCACCACGTATGCGACAAGACTGCCACGATATGACGAGAGCAAGTTACTATGTAAACATCACCTCTTTACACGAAGGGCCAGAACAGGAGAATCAGGTACGATAGTAAAAGTACCCGTGAAAATCGGGATACCACCTCGCAACCCCGCAGAGAGAAGGAGGCCAGCATGATTTTTGCGCTCCTCTACTGTGTGGAGAAGCTGCAACATGAAATGTTTCAGCTGTTTGTGTTTACTTCACGTTTTTAAGTGGGTTAGACGGGCGTTCACGTTGCGAACCTGGATTCGCCCGCCGGGCGGACTGTTGCAATGCGAGATTTTCCGACACCCAAATCCAAATAGCTAAGATTGCGAGGGTCTAATAAGGGTATCAGCGGGCCGACGACGCGGTCACACCTCATGTTAACGACGATGTCGTGACACACTCTGGAGTCATGTCCTACCCATGGATGTACAGGTTTTAAGTACATGAGCTCACCTGGCAGAGCCATCTTCTTCACTGCCCGTTTCACAGTACTGAGTGATAACAAGTGGCCATGATACCAAAGGTTAAGGGGGGCAGGAAATACCACGATAGTTCCGGACGAAGCGGGGTTCTACACGTGCCCAATCCCGTGCTTAAATCCGCGCAAGAACAGTACACGTCCTCTACTAGGCCTGTGCCTTAAAGGTCAGTGGATATTTGATGGTCACAGATAATCTCTATGAACGGCATCGACATTATCCTTTCCATCTATTAGGGTTGTGTGACCGAGACTGTTTACTTGGAGGGAATTCATTCTAGGACGTCCTTCTAGCCTTACTCTGTTTCGGCTCGGACTAGCTCAGGTCTTGGACTAGACCTTAGTGGGTTAAATTTCAAGTGCGGCTTATAGTGAGCTAGTCGAGTTACTCCTCCGATGGGCTAGTCTTTCACCGTCTTCCGGTAGAGGGAGCCGCATTCTCGAGATTATAGTCGTGAGATGGGCTCACTGTCCTGTGGAGAATAACCTCCGTGGAGAGTAGTTAACTATACCAGATACAGAGTTGATTTTGCTAAAGGATGGTAATATTTTGCTCTTAACGTTAAGGGTAACATCCACTGTCGGTCATACCTGAAGACCGGTATGATCCGATTTGTAAAGGGTTACTTCGCCTATTGTCCTGCCACCGCGACTCTTGATGCCAGAAGCCAATCGTTTTATTCTTTTCCCCCCGCTTACACGGTCAGGCCGAACTTGCTGGGCTCGTCATCTGGAGACTACTATTTTCTTACCGCACCGCGGGGTCCTACAGTAGACGAGTTGTGCATCGCCAACACTTGTAAATTAGGCGTAATGGCTGCAGCACGCCAATAGGGATTCTCAATTTACGGGGGTCCTGACAGGACGGTCCGGACACTCCTGGAACGTGCTGGAAGGGCACCTTAAGGGGCTGTGAATGTGAGGTCCGATACTCAAGACTTCCTGACTGCCCCTGATTTCGACGGAAGCCTTTTACTATTCATTACCGCCAACAGTCAGGGATGCTCCAAATACATAGCGAAGCGACAGCCTGCTGGTGCGCCGCGCCATGATGTTGGTCTTGCGAGCAGTAAATCCGTCTGCGTGATACGCTATACGGGAACGCTCATGCGCTCGCATCGACGGAGCTAGGCTCGTGATGCTCATCATCATCCTGGAAGGACGGCGTTGGATTTAGTAGGTGGCGTGACTCTCTTAGATTACGTCGGGAAGTCGTGTGAAAAGGTTCCAGCCAAGCGCAAAACGGGTGCCCATGCTATAGGAGCTCCCTGCCCTGGTTCGTGAACCTGGAGAGAGACCTTAGCAATTAAGTTCTGACCTTGCATAAATCCGTAACTATGGCCTGTTCGTCAGTGAGATATAACATTCAAGTATTCGGGTCTGTGGACAGCTTCACTCGGCTGGCAAGTAGCATCCGGGTGACATTGAGCCTTGTATCTTGGGAGTAGCTGAGAAATTTTGATGCGGCTGTCACGTCGTCCGAGCGTGTGTGTATGGGCTAGAGTCTACTAAAAGATGGGCGTGTATAACGGAAATCTGGCGGGCGCGTAGTCGAGAAACTAACATGAAGGTGCTACACACTTTAGGGTACCACGGGAGCATCCATCCACTCCGGACACTATGCAAGCTACGGCTGGCCTGACTAATATTCGTACGTTACGGCCCTTCTTCCTTAACCTGGGGTACGTTGGCGTTGCGTACCCACGCGACAGAAATTGCATATATCAGCAGGGACACGTACATGATACGTTAATAGTCTTTAAGGCCACCGCCAATATATGGGCCGCTAGCGACGTAAACGGCGCATTTCGATGGCGACGGATGCTCGCGCTTTCGCTACACCCCGGCCCCCAGCTAAGCCTCACGACTATGTGACCGCTTCCCGAGGCCCTTCTAAGGTCGGGCCCCTGTTAGGACGGGGGACAGCACACATCTGTAAGTAAAGCCTAAGGTCTAGATCCTACCTTTCTAATGACGTTTCCTCGGTTCTAGCGTACTTAACGTAGTACAATGACAGGGCATCGCGTATCGAAACTCGCGACTTCCAGAGATTCTAGATCGCGTATCGCTAAGAACCCCTGGGTCCAATCACTGGTAGGCAATAATTCCGACAAGATGGTCGCTTCTCCAGCCGGGTACCTTATACGTTAGGTTACTTGGGTGGCAGCTACGAACGCCCTTTAGGCCCGGCCTATTTCATCCTCTAACCCAGGCATATGTTTGGTTTGTATTTTTAGCAGAACGTAAACCCAATCATAAAGCTATAGTGTAAATCCAAGAGCAGCCATCGCCGCCGATCCTTCGAGGTAGATATATATGCCCATTGCCAATTAGCATATCATTGACACGGCCAAACCCCCGGACTTCTGCCTGCATACATACTCGACTTACAAGATAGCCGAAGGGCCTACGTTGACCACCTACGTCTGAAACAATAGAATTGCCAATTGTAGTATACTAGAGTGCGCACGAACGTTTCAGGAATAACTAGTTGTCGTCTCTCGGGGACCTTAAAGAAGGACAGCGTTTAGAGGATACCCACATACAACGGTCACATTGTTTCGTAGGTGAGGATATCTTCGTTTAGGTAGTCGAAATCGCACACATGTTGTTAACAGCACGCGCCTTAATGACAGACGCGCAACACTGCTGCACAGACACCCTTCCACTGGCTCTTTTCGCCAACTGGGACACCGGGCGCAATCGAAGTTCACCATGAACTTGGGCGCGAATCCGCTCGTGACCCGTGCCAGTAAAGGCTTAATGGTCTGGGCAAAGCAAAAGATTGGTCATGGCCAATGTTGTTTCGCCCCACGTACAGTCTGACTGTGACCCTAACGTTCTAAGGTGAAGCTCATGCTCGCGAGCCTTGAACCGGGTCGACGAAGGCTCTACGAAAAACGGACAGACCCACAGAAATTTAAACGCGAGTGAAATTAGGGCTTTTGTGCTGGCTGTGGATAAAAAGGTCGGGTCGATGAGGGTGCGTACACCCATAAAGTGATCGCTGTGAGCGATAGGCCTAAAAGGTGTATAGCGCGACGACAGACTCTATGAAGAATAGTCTTTAAACCCGGCAAGATCAGGTTGGTACCCCTCCAACGTAGGGGACATATTTGAGTCCTTCTTTGGCCGTTGACCCTAGAATAGAGTTTAACAAGTAGTACGAAACACGTATAATATTTACAGAAGTCGACGCACAGCTCCCGGAAGCCCCCACCAGCTAGAGTACGTACGAGTCATTCTGTGCGCACGTTACTCGTTTCCTACGCAAATAGGGTCTGGTGAAAGCTAGTCTTGGGGGGTTTCTGGCAGACTTGGAAGAAGGTACTAACCGCAAACGCGCTACGCTTCCATCGGTCATAACAACATACAAATGTTCTTCGCAACAAAGCCCTCTCCGTCCATAACATCACCATGGACGTCTCCACTTCCCTAGCATTACAATGCGCATAGTGGAATTATGC";

	public void testExpectation() {
		Alphabet dnaAlphabet = Alphabet.getDnaAlphabet();
		Alphabet iupacAlphabet = Alphabet.getIupacAlphabet();
		FiniteMemoryTextModel textModel = new MarkovianTextModel(2, dnaAlphabet, background);
		String pattern0 = "AGYMNCCT";
		String pattern1 = "ANNMACCT";		
		List<GeneralizedString> genStringList = new ArrayList<GeneralizedString>(1);
		genStringList.add(Iupac.toGeneralizedString(pattern0));
		genStringList.add(Iupac.toGeneralizedString(pattern1));
		CDFA cdfa = DFAFactory.build(dnaAlphabet, genStringList, 50000);
		assertEquals(2, cdfa.countMatches("AGTCACCT"));
		assertEquals(1, cdfa.countMatches("AGTCCCCT"));
		MatchCountDAA daa = new MatchCountDAA(cdfa, 2);
		PAA paa = new TextBasedPAA(daa, textModel);
		double[] dist = paa.computeValueDistribution(pattern0.length());
		double expectedExpectation = dist[1] + 2.0*dist[2];
		PatternExpectationCalculator pec0 = new ForwardPatternExpectationCalculator(textModel, Iupac.asGeneralizedAlphabet(), iupacAlphabet.buildIndexArray(pattern0));
		PatternExpectationCalculator pec1 = new ForwardPatternExpectationCalculator(textModel, Iupac.asGeneralizedAlphabet(), iupacAlphabet.buildIndexArray(pattern1));
		double actualExpectation = pec0.getPrefixExpectation(pattern0.length()) + pec1.getPrefixExpectation(pattern1.length());
		assertEquals(1.0, expectedExpectation/actualExpectation, 1e-10);
	}

}
